﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UIAStudio
{
    class Hotkey
    {
        public Hotkey(string name, IntPtr hWnd, ModKeys modifiers, Keys key, EventHandler handler)
        {
            m_name = name;
            m_hWnd = hWnd;
            m_modifiers = modifiers;
            m_key = key;
            m_eventHandler = handler;
        }

        public bool Register()
        {
            m_id = NativeMethods.GlobalAddAtom(m_name);
            
            bool bResult = NativeMethods.RegisterHotKey(m_hWnd, m_id, (uint)m_modifiers, (uint)m_key);
            if (!bResult)
            {
                Logger4net.Logger.Instance.Error("Fail to register hot key!");
                return false;
            }
            m_hotKeyList.Add(this);
            return bResult;
        }

        public void Unregister()
        {
            NativeMethods.UnregisterHotKey(m_hWnd, m_id);
        }

        public void Invoke()
        {
            m_eventHandler.Invoke(this, null);
        }

        private string m_name;
        private IntPtr m_hWnd;
        private ModKeys m_modifiers;
        private Keys m_key;
        private int m_id;
        private EventHandler m_eventHandler;

        private static List<Hotkey> m_hotKeyList = new List<Hotkey>();
    }
}
